<?php
class AdminAction extends BaseAction{
	
	  
    public function adminList(){
    	
    	import("ORG.Util.Page"); // 导入分页类   
      $count = M('Admin')->field('id')->count(); // 查询满足要求的总记录数   
      $Page  = new Page($count,10); // 实例化分页类 传入总记录数和每页显示的记录数   
      $show  = $Page->show(); // 分页显示输出   
      $list  = M('Admin')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();   
      $this->assign('list',$list); // 赋值数据集   
      $this->assign('page',$show); // 赋值分页输出
    	
      $this->display();
    }
    
    public function add(){
    	$this->display();
    }
    
    public function doAdmin(){
    	
				$user = D('Admin');
				if($user->create()){
					$user->password = md5($user->password);
					$user->ctime = time();
					$user->etime = time();
					Load('extend'); //ip 加载函数库
					$user->eip = get_client_ip();
					if($user->add()){
							$this->assign("jumpUrl","__URL__/adminList");
					    $this->success("添加管理员成功！");
						}else{
							$this->error('添加失败！');
						}
				}else{
					$this->error($user->getError());
				}
    }
    public function del(){
    	$return = M('Admin')->where('id='.$_GET['id'])->delete();
    	if($return){
    		$this->success("删除管理员成功！");
    	}else{
    		$this->error("删除管理员失败！");
    	}
    }
    
    public function edit(){
    	
    	$admin = M('Admin')->where('id='.$_GET['aid'])->find();
    	$this->assign('admin',$admin);
    	
    	$this->display();
    	
    }
    
    public function editAdmin(){
				
				if(!empty($_POST['password'])){
					$admin = M('Admin')->where('id='.$_POST['id'].' and password="'.md5($_POST['npassword']).'"')->find();
					if($admin){
						$this->error('旧密码错误!');
						return;
					}
				}
				$user = D('Admin');
				if($user->create()){
					if(!empty($_POST['password'])){
						$user->password = md5($_POST['password']);
					}
					$this->assign("jumpUrl","__URL__/edit/aid/".$_POST['id']);
					if($user->save()){
					    $this->success("修改管理员成功！");
						}else{
							$this->error('修改失败！');
						}
				}else{
					$this->error($user->getError());
				}
				
    }
    
   public function doAudit()
	 {
			$model = M('Admin');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('status',1)){
						$this->assign("jumpUrl", "__URL__/adminList");
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 public function doUp()
	 {
			$model = M('Admin');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('status',0)){
						$this->assign("jumpUrl", "__URL__/adminList");
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
    
}
?>